# tcam-capture completion                             -*- shell-script -*-

# Copyright 2018 The Imaging Source Europe GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


_tcam-capture () {

    local cur prev words cword
    _init_completion || return

    local COMMANDS=(
        "--help"
        "-h"
        "--help-all"
        "-v"
        "--version"
        "--serial"
    )

    local command i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${COMMANDS[@]} =~ ${words[i]} ]]; then
            command=${words[i]}
            break
        fi
    done

    case $prev in
        --serial)
            # explicitly use --gst-debug=*:0
            # we do not want debug output for tab completion
            COMPREPLY=( $( compgen -W "$( tcam-ctrl --list-serial-long --gst-debug=*:0)" -- "$cur" ) )
            return 0
            ;;
    esac

    # complete using basic options
    #COMPREPLY=( $( compgen -W '-h --help --reset --serial --format -v --fullscreen' -- "$cur" ) )


    if [[ ${#COMPREPLY[@]} -ne 0 ]]; then
        # this removes any options from the list of completions that have
        # already been specified somewhere on the command line, as long as
        # these options can only be used once (in a word, "options", in
        # opposition to "tests" and "actions", as in the find(1) manpage).
        local -A onlyonce=( [--serial]=1 [--format]=1 [--help]=1 [-h]=1 [-v]=1 [--version]=1
                             )
        local j
        for i in "${words[@]}"; do
            [[ $i && ${onlyonce[$i]} ]] || continue
            for j in ${!COMPREPLY[@]}; do
                [[ ${COMPREPLY[j]} == $i ]] && unset 'COMPREPLY[j]'
            done
        done
    fi


    # no command yet, show what commands we have
    if [ "$command" = "" ]; then
        COMPREPLY=( $( compgen -W '${COMMANDS[@]}' -- "$cur" ) )
    fi

    return 0
} &&
    complete -F _tcam-capture tcam-capture
